#include "at24cxx.h"


HAL_StatusTypeDef AT24Cxx_IsDeviceReady()
{
	uint32_t Trials=10;//尝试次数
	HAL_StatusTypeDef result = HAL_I2C_IsDeviceReady(&I2C_HANDLE,DEV_ADDR_24CXX,Trials,AT24C_TIMEOUT);
	
	return result;

}



HAL_StatusTypeDef  AT24Cxx_WriteOneByte(uint16_t memaddress , uint8_t byteData)
{
	HAL_StatusTypeDef result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX,memaddress,AT24C_MEMADD_SIZE,&byteData,1,AT24C_TIMEOUT);
	
	return result;

}



HAL_StatusTypeDef  AT24Cxx_ReadOneByte(uint16_t memaddress , uint8_t *byteData)
{
	HAL_StatusTypeDef result = HAL_I2C_Mem_Read(&I2C_HANDLE,DEV_ADDR_24CXX,memaddress,AT24C_MEMADD_SIZE,byteData,1,AT24C_TIMEOUT);
	
	return result;
	
}

HAL_StatusTypeDef  AT24Cxx_ReadBytes(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen)
{
	if(bufferLen > MEM_SIZE_24CXX)//超过总存储容量
	{
		return HAL_ERROR;
	}
	
	HAL_StatusTypeDef result = HAL_I2C_Mem_Read(&I2C_HANDLE,DEV_ADDR_24CXX,memaddress,AT24C_MEMADD_SIZE,pBuffer,bufferLen,AT24C_TIMEOUT);
	
	return result;
}



HAL_StatusTypeDef  AT24Cxx_WriteInonePage(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen)
{
	if(bufferLen > PAGE_SIZE_24CXX)//数据长度不能大于页的大小
	{
		return HAL_ERROR;
	}
	
	HAL_StatusTypeDef result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pBuffer,bufferLen,AT24C_TIMEOUT);
	
	return result;
	
}



HAL_StatusTypeDef  AT24Cxx_WriteLongData(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen)
{
	
	if(bufferLen > MEM_SIZE_24CXX)//超过总存储容量
	{
		return HAL_ERROR;
	}
	
	HAL_StatusTypeDef result = HAL_ERROR;
	
	if(bufferLen < PAGE_SIZE_24CXX)//不超过1个Page,直接写入后退出
	{
		HAL_StatusTypeDef result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pBuffer,bufferLen,AT24C_TIMEOUT);
		HAL_Delay(10);//必须有延时，已等待页写完
		return result;
	}
	
	
	uint8_t   *pt = pBuffer;//临时指针，不能改变传入的指针
	uint16_t  pageCount = bufferLen/PAGE_SIZE_24CXX;//Page个数
	
	for(uint16_t i=0;i<pageCount;i++)//一次写入一个Page的数据
	{
		result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pt,PAGE_SIZE_24CXX,AT24C_TIMEOUT);
		pt += PAGE_SIZE_24CXX;
		memaddress+=PAGE_SIZE_24CXX;
		HAL_Delay(10);//必须有延时，已等待页写完
		
		if(result != HAL_OK)
		{
			return result;
			
		}
		
				
	}
	
	
	uint16_t leftBytes = bufferLen % PAGE_SIZE_24CXX ;//余数
	
	if(leftBytes>0)//写入剩余数据
	{
		result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pt,leftBytes,AT24C_TIMEOUT);
		HAL_Delay(10);//必须有延时，已等待页写完
	}
	return result;
	
}



HAL_StatusTypeDef AT24Cxx_Arbitrary_Write_Cmd(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen)
{
	volatile uint16_t EarseStartAddr=0,RemainWritePageNum=0,CurrentPosition=0,CurrentPageFreeSpace=0,leftBytes=0;
	uint8_t   *pt = pBuffer;//临时指针，不能改变传入的指针
	volatile uint16_t  RemainWriteNum=0;
	
	if(bufferLen > MEM_SIZE_24CXX)//超过总存储容量
	{
		return HAL_ERROR;
	}
	
	
	if(bufferLen < PAGE_SIZE_24CXX)//不超过1个Page,直接写入后退出
	{
		HAL_StatusTypeDef result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pBuffer,bufferLen,AT24C_TIMEOUT);
		HAL_Delay(10);//必须有延时，已等待页写完
		return result;
	}
	
	
	
	//获取写入数据起始地址在当前页的位置
	CurrentPosition = memaddress % PAGE_SIZE_24CXX;//起始地址为0，实际位置 = CurrentPosition+1
	
	//统计当前页能写入数据的个数
  CurrentPageFreeSpace = PAGE_SIZE_24CXX - CurrentPosition;
	
	HAL_StatusTypeDef result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pBuffer,CurrentPageFreeSpace,AT24C_TIMEOUT);
	HAL_Delay(10);//必须有延时，已等待页写完

	pt += CurrentPageFreeSpace;
	memaddress += CurrentPageFreeSpace;
	
	//剩余写入数据个数
	RemainWriteNum = bufferLen - CurrentPageFreeSpace;
	
	//获取剩余整页写入次数
	RemainWritePageNum = (RemainWriteNum / PAGE_SIZE_24CXX);
	
	for(uint8_t i =0;i<RemainWritePageNum;i++)
	{
		if(result == HAL_OK)
		{
				result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pt,PAGE_SIZE_24CXX,AT24C_TIMEOUT);
				pt += PAGE_SIZE_24CXX;
				memaddress += PAGE_SIZE_24CXX;
			  HAL_Delay(10);//必须有延时，已等待页写完
		 }
		else
		{
			return result;
		}

	}
	

  leftBytes = RemainWriteNum % PAGE_SIZE_24CXX ;//余数
	
	if(leftBytes>0)//写入剩余数据
	{
		result = HAL_I2C_Mem_Write(&I2C_HANDLE,DEV_ADDR_24CXX, memaddress,AT24C_MEMADD_SIZE,pt,leftBytes,AT24C_TIMEOUT);
		
	}
	
	return result;

}




HAL_StatusTypeDef AT24Cxx_EarseAll_Cmd()
{
	uint8_t  EarseArry[0xFF];
	uint16_t memAddr=0;
	HAL_StatusTypeDef result;
	
	for(uint16_t i= 0;i<PAGE_SIZE_24CXX;i++)
	{
		EarseArry[i] = 0xFF;
		
	}
	
	for(uint16_t i=0;i<PAGE_NUM;i++)
	{
		result = AT24Cxx_WriteInonePage(memAddr,EarseArry,PAGE_SIZE_24CXX);
		memAddr+=PAGE_SIZE_24CXX;
		HAL_Delay(10);
	}
	
	return result;
}





